// TOWN SCRIPT
//    Town 1: First Floor

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documentation. States you write
// yourself should be numbered from 10-100.

// flags:
//  1, 7  = Gate flag via lever in graveyard.
//  1, 14 = door unlocked by pool.
//  1, 15 = door unlocked by stairs.
//  1, 16 = door unlocked in storeroom.
//  1, 17 = door unlocked by garden.
//

begintownscript;

variables;

short choice, x, cc;

body;

beginstate INIT_STATE;
	turn_off_training(TRUE);
	set_crime_tolerance(5);

	if (get_flag(1, 1) > 0) {
		set_terrain(60, 56, 397);
	}

	set_name(12, "Dan");
	set_name(13, "Liz");
	set_name(14, "Ellen");
	set_name(15, "Dan Jr.");

	// make visible family for end game animation...
	if ((get_flag(100, 0) == 4) && (get_flag(50, 1) == 0)) {
		activate_hidden_group(10);
	}
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
break;

beginstate 10;
	block_entry(TRUE);
	if (char_loc_x(0) == 34) {
		x = 22;
	}
	else {
		x = 23;
	}
	move_to_new_town(0, x, 25);
break;

beginstate 11;
	if (get_flag(1, 3) == 0) {
		set_flag(1, 3, 1);
		message_dialog("You step into a large entrance chamber.", "A statue seems to glow with inner energy in the middle of the room.");
	}
break;

beginstate 12;
	turn_off_training(FALSE);
	if (get_flag(1, 0) == 0) {
		set_flag(1, 0, 1);
		message_dialog("This appears to be some sort of training facility.", "You could probably get some exercise if you wanted.");
	}
break;

beginstate 13;
	turn_off_training(TRUE);
break;

beginstate 14;
	if (get_flag(1, 1) == 0) {
		if ((has_item(318)) && (has_item(161))) {
			reset_dialog();
			add_dialog_str(0, "The ground seems a little loose here.", 0);
			add_dialog_str(1, "Dig up some dirt and fill your bucket?", 0);
			add_dialog_choice(0, "Yes");
			add_dialog_choice(1, "No");
			choice = run_dialog(1);
			if (choice == 1) {
				block_entry(TRUE);
				set_flag(1, 1, 1);
				play_sound(79);
				take_item(161);
				reward_give(453);
				set_terrain(60, 56, 397);
				award_party_xp(200, 25);
			}
		}
	}
	else {
		block_entry(FALSE);
	}
break;

beginstate 15;
	if (get_flag(1, 2) == 0) {
		set_flag(1, 2, 1);
		message_dialog("This appears to be a cloak room.  It smells musty.", "Empty hooks line the walls and a few ragged items sit on a table in the back.");
	}
break;

beginstate 16;
	if (get_flag(1, 4) == 0) {
		set_flag(1, 4, 1);
		message_dialog("This is the manor's chapel.", "It seems to be strangely quiet.");
	}
break;

beginstate 17;
	if (get_flag(1, 5) == 0) {
		set_flag(1, 5, 1);
		message_dialog("The manor's graveyard seems almost out of place.", "Most of the graves look old, except for one with freshly turned dirt.");
	}
break;

beginstate 18;
	if (get_flag(1, 6) == 0) {
		set_flag(1, 6, 1);
		message_dialog("There still seem to be some livestock within the manor's paddock, though no horses.", "A barn lies in the distance on the other side of the meadow.");
	}
break;

beginstate 19;
	if (get_flag(1, 8) == 0) {
		set_flag(1, 8, 1);
		message_dialog("The room seems to be some sort of laboratory.  Strangely, there's a bedroll against the far wall.", "It's not clear about the type of experiments that were carried on here.");
	}
break;

beginstate 20;
	if (get_flag(1, 9) == 0) {
		set_flag(1, 9, 1);
		message_dialog("The room on the other side of the pool seems to be a guest room.", "From the look of the accumulated dust and cobwebs, it hasn't been used in a while.");
	}
break;

beginstate 21;
	block_entry(TRUE);
	move_to_new_town(2, 21, 27);
break;

beginstate 22;
	block_entry(TRUE);
	// rod, dirt, towel, fan, flute (most important!)
	if ((has_item(452)) && (has_item(453)) && (has_item(454)) && (has_item(455)) &&
	    (has_item(462))) {
		x = TRUE;
	}
	else {
		if (get_flag(50, 6) == 0)
			x = TRUE; // haven't discovered how to activate vortex's yet
		else
			x = FALSE;
	}
	if (get_flag(50, 1) > 0) x = TRUE; // already won...
	if (x != TRUE) {
		reset_dialog();
		add_dialog_str(0, "The stairs lead to the upper floors of the manor.", 0);
		add_dialog_str(1, "You appear to be missing some items you may need to survive or complete your task.", 0);
		add_dialog_str(2, "Continue up the stairs anyway?", 0);
		add_dialog_choice(0, "No");
		add_dialog_choice(1, "Yes");
		choice = run_dialog(1);
		if (choice == 2) {
			x = TRUE;
		}
	}
	if (x == TRUE) {
		move_to_new_town(3, 24, 29);
	}
break;

beginstate 23;
	if (get_flag(1, 10) == 0) {
		set_flag(1, 10, 1);
		message_dialog("You feel a slight breeze coming from the south.", "");
	}
break;

beginstate 24;
	if ((get_flag(50, 3) > 0) && (get_flag(50, 9) == 0)) {
		set_flag(50, 9, 1);
		message_dialog("Behind some books on a shelf, you find a tattered journal.", "");
		reward_give(458);
		award_party_xp(100, 25);
	}
	else {
		set_state_continue(25);
	}
break;

beginstate 25;
	message_dialog("The bookshelves contain lots of novels and manuals along with other exotic texts.", "The family appears to have been well read, though these books won't help currently.");
break;

beginstate 26;
	if (get_flag(1, 11) == 0) {
		set_flag(1, 11, 1);
		reset_dialog();
		add_dialog_str(0, "The dimly lit room has 10 doors on the eastern wall.", 0);
		add_dialog_str(1, "The doors are numbered 1 to 10 starting with the northernmost one.", 0);
		add_dialog_str(2, "The sound of the wind breaking against the cliffs behind the manor is quite strong and loud in here.", 0);
		add_dialog_choice(0, "Ok");
		choice = run_dialog(1);
	}
break;

beginstate 27;
	if (get_flag(50, 5) == 0) {
		block_entry(TRUE);
	}
	else {
		block_entry(FALSE);
		if (get_flag(1, 12) == 0) {
			set_flag(1, 12, 1);
			message_dialog("As stated in the journal, you find a hidden wall in the corridor.", "You wonder what the nature of the traps behind it will be.");
		}
	}
break;

beginstate 28;
	if ((get_flag(100, 0) == 4) && (get_flag(50, 1) == 0)) {
		set_flag(50, 1, 1);
		message_dialog("You are a little startled to see the family waiting by the statue.", "Dan waves at you as you enter the room and walk over to them.");
		x = 36;
		if (party_size() == 2) {
			x = 35;
		}
		else if (party_size() == 1) {
			x = 34;
		}
		teleport_party(x, 51, 1);
		force_view_center(34, 51);
		set_character_facing(1000, 0);
		force_instant_terrain_redraw();
		pause(5);

		reset_dialog();
		add_dialog_str(0, "Dan starts speaking 'Thank you for rescuing my family and me.'", 0);
		add_dialog_str(1, "'I was sucked in just as I set the high frequency trap for the one in the attic.' he adds.", 0);
		add_dialog_str(2, "'Without your help, we would still be trapped inside the vortexes.'", 0);
		add_dialog_str(3, "'I am aware of a reward you've been offered, please accept this gold for your efforts.'", 0);
		add_dialog_str(4, "Dan hands you a bag of gold with gratitude in his eyes.", 0);
		add_dialog_choice(0, "Ok");
		choice = run_dialog(1);
		play_sound(38);
		if (coins_amount() <= 15000) {
			change_coins(15000);
		}
		else {
			cc = 30000 - coins_amount();
			// may only get the emperors new gold (if already have 30000)...
			if (cc > 0) change_coins(cc);
		}
		// 3000 exp
		award_party_xp(1000, 30);
		award_party_xp(1000, 30);
		award_party_xp(1000, 30);

		reset_dialog();
		add_dialog_str(0, "Dan continues 'I have also fixed this statue so that it does not take your money when you use it.'", 0);
		add_dialog_str(1, "'Please feel free to take advantage of it if you wish.' he adds.", 0);
		add_dialog_str(2, "'We need to go let our relatives know we are Ok, so this is goodbye.' Dan says.", 0);
		add_dialog_str(3, "With a hearty hand-shake, Dan, Liz and the kids thank you again and then walk off.", 0);
		add_dialog_choice(0, "Ok");
		choice = run_dialog(1);

		text_bubble_on_char(15, "Thank you");
		force_instant_terrain_redraw();
		pause(15);
		text_bubble_on_char(15, "");
		force_instant_terrain_redraw();

		text_bubble_on_char(14, "Thank you");
		force_instant_terrain_redraw();
		pause(15);
		text_bubble_on_char(14, "");
		force_instant_terrain_redraw();

		text_bubble_on_char(13, "Thank you");
		force_instant_terrain_redraw();
		pause(15);
		text_bubble_on_char(13, "");
		force_instant_terrain_redraw();

		text_bubble_on_char(12, "Thank you again");
		force_instant_terrain_redraw();
		pause(15);
		text_bubble_on_char(12, "");
		force_instant_terrain_redraw();

		// family walks away
		set_state_continue(30);
	}
break;

beginstate 29;
	if ((get_flag(1, 13) == 0) && (get_flag(50, 5) > 0)) {
		set_flag(1, 13, 1);
		message_dialog("As you throughly search the bookshelves, you find a torn piece of paper stuck to a nail in the back.", "You carefully extract it so it doesn't get any more damage.");
		reward_give(461);
		award_party_xp(100, 25);
	}
break;

beginstate 30;
	relocate_character(14, 37, 51);
	relocate_character(13, 36, 50);
	relocate_character(12, 35, 50);
	relocate_character(15, 34, 50);
	force_instant_terrain_redraw();
	pause(5);

	relocate_character(14, 37, 52);
	relocate_character(13, 37, 51);
	relocate_character(12, 36, 50);
	relocate_character(15, 35, 50);
	force_instant_terrain_redraw();
	pause(5);

	relocate_character(14, 36, 53);
	relocate_character(13, 37, 52);
	relocate_character(12, 37, 51);
	relocate_character(15, 36, 50);
	force_instant_terrain_redraw();
	pause(5);

	relocate_character(14, 35, 54);
	relocate_character(13, 36, 53);
	relocate_character(12, 37, 52);
	relocate_character(15, 37, 51);
	force_instant_terrain_redraw();
	pause(5);

	relocate_character(14, 35, 55);
	relocate_character(13, 35, 54);
	relocate_character(12, 36, 53);
	relocate_character(15, 37, 52);
	force_instant_terrain_redraw();
	pause(5);

	relocate_character(14, 35, 56);
	relocate_character(13, 35, 55);
	relocate_character(12, 35, 54);
	relocate_character(15, 36, 53);
	force_instant_terrain_redraw();
	pause(5);

	relocate_character(14, 35, 57);
	relocate_character(13, 35, 56);
	relocate_character(12, 35, 55);
	relocate_character(15, 35, 54);
	force_instant_terrain_redraw();
	pause(5);

	text_bubble_on_char(14, "Bye Bye");
	force_instant_terrain_redraw();
	pause(15);
	text_bubble_on_char(14, "");
	force_instant_terrain_redraw();
	erase_char(14);
	force_instant_terrain_redraw();

	relocate_character(13, 35, 57);
	relocate_character(12, 35, 56);
	relocate_character(15, 35, 55);
	force_instant_terrain_redraw();
	pause(5);

	text_bubble_on_char(13, "Bye");
	force_instant_terrain_redraw();
	pause(15);
	text_bubble_on_char(13, "");
	force_instant_terrain_redraw();
	erase_char(13);
	force_instant_terrain_redraw();

	relocate_character(12, 35, 57);
	relocate_character(15, 35, 56);
	force_instant_terrain_redraw();
	pause(5);

	text_bubble_on_char(12, "See ya later");
	force_instant_terrain_redraw();
	pause(15);
	text_bubble_on_char(12, "");
	force_instant_terrain_redraw();
	erase_char(12);
	force_instant_terrain_redraw();

	relocate_character(15, 35, 57);
	force_instant_terrain_redraw();
	pause(5);

	text_bubble_on_char(15, "Bye Bye");
	force_instant_terrain_redraw();
	pause(15);
	text_bubble_on_char(15, "");
	force_instant_terrain_redraw();
	erase_char(15);
	force_instant_terrain_redraw();
break;
